import {
  getMessageTable,
  editMessageRecord
} from '../../api/system'
import {
  MESSAGE_LIST,
  MESSAGE_READ_LIST,
  MESSAGE_CURRENT_ROW,
  MESSAGE_DATA_COUNT,
  MESSAGE_HISTORY_DATA,
  MESSAGE_PAGE_SIZE,
  UNREAD_MESSAGE_COUNT
} from '../motations-type'

export default {
  state: {
    messageList: [],
    messageReadList: null,
    messageDataCount: null,
    unReadMessageCount: null,
    messagePageSize: 20,
    messageHistoryData: [],
    row: {
      time: '',
      title: '',
      sender: '',
      summary: '',
      state: ''
    }
  },
  getters: {
    messageList: state => state.messageList
  },
  mutations: {
    [MESSAGE_LIST] (state, data) {
      state.messageList = data
    },
    [MESSAGE_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [MESSAGE_DATA_COUNT] (state, data) {
      state.messageDataCount = data
    },
    [MESSAGE_HISTORY_DATA] (state, data) {
      state.messageHistoryData = data
    },
    [MESSAGE_PAGE_SIZE] (state, data) {
      state.messagePageSize = data
    },
    [MESSAGE_READ_LIST] (state, data) {
      state.messageReadList = data
    },
    [UNREAD_MESSAGE_COUNT] (state, data) {
      state.unReadMessageCount = data
    }
  },
  actions: {
    async getMessageData ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let unreadcount = null
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getMessageTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          title: item.title,
          ctime: item.ctime,
          // sender: item.sender,
          content: item.content,
          state: item.state
        }]
        if (item.state === 0) {
          unreadcount += 1
        }
      })
      commit(MESSAGE_LIST, newData)
      commit(UNREAD_MESSAGE_COUNT, unreadcount)
      commit(MESSAGE_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.message.messagePageSize) {
        commit(MESSAGE_HISTORY_DATA, newData)
      } else {
        commit(MESSAGE_HISTORY_DATA, newData.slice(0, rootState.message.messagePageSize))
      }
      return resp
    },
    async editMessageData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editMessageRecord(data)
      // console.log(resp)
      commit(MESSAGE_READ_LIST, resp.data.data)
    },
    async getMessageCurrentRow ({ commit, rootState }, row) {
      commit(MESSAGE_CURRENT_ROW, row)
    },
    async changeMessagePage ({ commit, rootState }, index) {
      let data = rootState.message.messageList
      let _start = (index - 1) * rootState.message.messagePageSize
      let _end = index * rootState.message.messagePageSize
      commit(MESSAGE_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeMessagePageSize ({ commit, rootState }, value) {
      commit(MESSAGE_PAGE_SIZE, value)
    }
  }
}
